<?php
namespace app\oem\controller;
use think\Controller;
use think\Db;
use app\common\controller\OemBaseController;

class Index extends OemBaseController
{
	
    public function index()
    {
    	$id=session('d_id');
    	$data = Db::table('info_oem')->where('id',$id)->find();
    	$data['balance'] 	 = substr($data['balance'],0,strlen($data['balance'])-2);
    	$data['commission'] 	 = substr($data['commission'],0,strlen($data['commission'])-2);
    	$this->assign('data',$data);
    	return $this->fetch();
    }
    public function child_account()
    {
    	$user_level=session("user_level");
		if($user_level==1){//oem
			$where['oem_id']=session('d_id');
			$where['user_type']=["neq",0];
			$where['user_level']=1;
		}elseif($user_level==2){//省
			$where['agency_id']=session('d_id');
			$where['user_type']=["neq",0];
			$where['user_level']=2;
		}elseif($user_level==3){//市
			$where['city_id']=session('d_id');
			$where['user_type']=["neq",0];
			$where['user_level']=3;
		}
		
    	$list=db('info_oem')->where($where)->select();
		$this->assign("list",$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
	public function add_account(){
		if(request()->isPost()){
			$is_reg=db('info_oem')->where("mobile",input("mobile"))->find();
			if($is_reg){
				return json(['code'=>402,'msg'=>'添加失败,该手机号码已经添加过']);
			}
			$param['add_time'] 	= now_time();
			$param['user_type'] 	= input("user_type");
			$param['mobile'] 	= input("mobile");
			$param['user_name'] 	= input("user_name");
			$param['password'] 	= md5_salt(input("password"));
			$param['status'] 	= input("status");
			$param['user_level']  	= session('user_level');
			$user_level=session("user_level");
			if($user_level==1){//oem
				$param['oem_id']=session('d_id');
			}elseif($user_level==2){//省
				$param['oem_id']=session('oem_id');
				$param['agency_id']=session('d_id');
			}elseif($user_level==3){//市
				$param['oem_id']=session('oem_id');
				$param['agency_id']=session('agency_id');
				$param['city_id']=session('d_id');
			}
		
			$res = db('info_oem')->insert($param);
			if($res){
				return json(['code'=>200,'msg'=>'添加成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'添加失败']);
			}
			
    	}
		return $this->fetch();
	}
	public function edit_account(){
		if(request()->isPost()){
			$param['user_type'] 	= input("user_type");
			$param['mobile'] 	= input("mobile");
			$param['user_name'] 	= input("user_name");
			if(input("password")){
				$param['password'] 	= md5_salt(input("password"));
			}
			$param['status'] 	= input("status");
			$param['id']=input("id");
			$res = db('info_oem')->update($param);
			if($res){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
    	}
		$account=db('info_oem')->find(input("id"));
		$this->assign('ad',$account);
		return $this->fetch();
	}
	
	
	public function renzheng(){
		$account=db('sys_member_verif')->where('user_id',session("d_id"))->find();
		$this->assign('account',$account);
		if(request()->isPost()){
			$param['id_card_front'] 	= input("id_card_front");
			$param['id_card_behind'] 	= input("id_card_behind");
			$param['bank_card_front'] 	= input("bank_card_front");
			$param['half_photo'] 		= input("half_photo");
			$param['user_type'] 		= 2;
			$param['user_name'] 		= input("user_name");
			$param['bank_name'] 		= input("bank_name");
			$param['bank_no'] 			= input("bank_no");
			$param['id_number'] 		= input("id_number");
			$param['reserve_mobile'] 	= input("reserve_mobile");
			$param['state'] 			= 2;
			$param['oem_id'] 			= session("oem_id");
			if($account){
				$ress = db('sys_member_verif')->where('user_id',session("d_id"))->update($param);
			}else{
				$param['add_time'] 	= now_time();
				$ress = db('sys_member_verif')->insert($param);
			}
			if($ress){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
    	}
		return $this->fetch();
	}
	
	
	//余额记录
	public function yue_record(){
		if(request()->isPost()){
    		$param=input('post.');
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
    	}
		$user_level=session("user_level");
		if($user_level==1){//oem
			$where['oem_id']=session('d_id');
		}elseif($user_level==2){//省
			$where['agency_id']=session('d_id');
		}elseif($user_level==3){//市
			$where['city_id']=session('d_id');
		}
		$where['order_type'] = ['not in','3'];
		//余额用户类型
		$where['user_type'] = session("user_level");
		$list=db('log_oem_charge')->where($where)->paginate(20);
		$this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
		return $this->fetch();
	}
	//佣金记录
	public function yongj_record(){
		if(request()->isPost()){
    		$param=input('post.');
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
    	}
		$user_level=session("user_level");
		if($user_level==1){//oem
			$where['oem_id']=session('d_id');
		}elseif($user_level==2){//省
			$where['agency_id']=session('d_id');
		}elseif($user_level==3){//市
			$where['city_id']=session('d_id');
		}
		//佣金用户类型
		$where['user_type'] = session("user_level");
		$list=db('log_oem_commission')->where($where)->paginate(10);
		$this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
		return $this->fetch();
	}
	//佣金转余额
	public function yongj_yue(){
		//获取当前用户信息
		$oem_info = db('info_oem')->where('id',session('d_id'))->find();
		if(request()->isPost()){
			// 启动事务
			Db::startTrans();			
			try{
				//查询佣金
				if(bccomp($oem_info['commission'], $_POST['commission'], 4)>=0){
					//计算佣金转为余额的金额
					$change_val = bcsub($_POST['commission'], bcmul($_POST['commission'], 0.05, 4), 4);
					
					$new_commission = bcsub($oem_info['commission'], $_POST['commission'], 4);
					$new_balance = bcadd($oem_info['balance'],$change_val, 4);
					//更新用户信息
					db('info_oem')->where('id', $oem_info['id'])->update(['balance' => $new_balance,'commission'=>$new_commission]);
					//oem佣金变化日志
					commission_change($oem_info['id'],0,5,$_POST['commission'],
					2,"佣金转余额扣除佣金",0,$oem_info['oem_id'],$oem_info['agency_id'],$oem_info['city_id']);
					//oem余额变化日志
					balance_change($oem_info['id'],0,2,$change_val,1,"佣金转余额增加余额",
					0,$oem_info['oem_id'],$oem_info['agency_id'],$oem_info['city_id']);
					// 提交事务
    				Db::commit();
					return json(['code'=>200,'msg'=>'转入成功']);
				}else{
					return json(['code'=>402,'msg'=>'转入失败']);
				}
			}catch ( Exception $e ) {
			    //$e->getMessage();
			    //回滚
			    Db::rollback();
			    exit;
			}
		}
		$this->assign('oem_info',$oem_info);
		
		return $this->fetch();
	}
	
	//提现
	public function yue_tixian(){
		//获取oem信息
		$oem_info = db('info_oem')->where('id',session('d_id'))->find();
		
		if(request()->isPost()){
			// 启动事务
			Db::startTrans();			
			try{
				//查询余额
				if(bccomp($oem_info['balance'], $_POST['balance'], 4)>=0){
					//更新用户信息
					db('info_oem')->where('id', $oem_info['id'])->setDec('balance',$_POST['balance']);
					//oem余额变化日志
					balance_change($oem_info['id'],0,1,$_POST['balance'],2,"余额提现",
					0,$oem_info['oem_id'],$oem_info['agency_id'],$oem_info['city_id']);
					//获取用户认证信息
					$member_verif = Db::table('sys_member_verif')->where('user_type', 2)->where('user_id',$oem_info['id'])->find();
					
					//提现日志
					withdrawals_log($oem_info['id'],3,$_POST['balance'],$_POST['balance'],0,1,
		    		"oem用户申请提现",0,0,0,0,$member_verif['bank_name'],$member_verif['bank_no'],$member_verif['user_name'],
		    		$member_verif['reserve_mobile'],$oem_info['oem_id']);
					 
					// 提交事务
    				Db::commit();
					 
					return json(['code'=>200,'msg'=>'提交成功']);
				}else{
					return json(['code'=>402,'msg'=>'提交失败']);
				}
			}catch ( Exception $e ) {
			    //$e->getMessage();
			    //回滚
			    Db::rollback();
			    exit;
			}
		}
		
		
		$this->assign('oem_info',$oem_info);
		
		return $this->fetch();
	}
	//修改登陆密码
	public function return_password(){
		if(request()->isPost()){
			$oemid=session('d_id');
			$oem_info = Db::table('info_oem')->where('id',$oemid)->find();
			if($oem_info['password'] ==md5_salt(input('jiupwd'))){
				$result = Db::table('info_oem')->where('id', $oem_info['id'])->update(['password' => md5_salt(input("xinpwd"))]);
				if($result){
					return json(['code'=>200,'msg'=>'修改成功']);
				}else{
					return json(['code'=>402,'msg'=>'修改失败']);
				}
			}else{
				return json(['code'=>408,'msg'=>'旧密码错误']);
			}
		}
		return $this->fetch();
	}
	//修改信息
	public function show_agent()
    {
    	$data=db('info_oem')->where('id',session("d_id"))->find();
		$this->assign('data',$data);
		if(request()->isPost()){
			$param['logo'] 	= input("logo");
			$param['project_name'] 	= input("project_name");
			$param['addres'] 	= input("addres");
			$ress = db('info_oem')->where('id',session("d_id"))->update($param);
			if($ress){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
    	}
    	return $this->fetch();
    }
	
	//签约流程1
	public function qianyue_step1()
    {
    	$data=db('agent_qianyue')->where('agent_id',input("agent_id"))->find();
		$this->assign('data',$data);
		$typelist=db('sys_platform_type')->where('pid',0)->select();
		$this->assign('typelist',$typelist);
		$this->assign('agent_id',input("agent_id"));
		if(request()->isPost()){
			$param['agent_type'] 	= input("agent_type");
			$param['agent_name1'] 	= input("agent_name1");
			$param['agent_name2'] 	= input("agent_name2");
			$param['trade_type'] 	= input("trade_type");
			$param['address'] 		= input("address");
			$param['service_tel'] 	= input("service_tel");
			$param['contacts'] 		= input("contacts");
			$param['job'] 			= input("job");
			$param['contact_tel'] 	= input("contact_tel");
			$param['email'] 		= input("email");
			if($data){
				$ress = db('agent_qianyue')->where('agent_id',input("agent_id"))->update($param);
			}else{
				$param['add_time'] 	= now_time();
				$param['agent_id'] 	= input("agent_id");
				$param['oem_id'] 	= session("oem_id");
				$ress = db('agent_qianyue')->insert($param);
			}
			if($ress){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
    	}
    	return $this->fetch();
    }
	//签约流程2
    public function qianyue_step2()
    {
    	$data=db('agent_qianyue')->where('agent_id',input("agent_id"))->find();
		$this->assign('data',$data);
		$this->assign('agent_id',input("agent_id"));
		if(request()->isPost()){
			$param['regist_name'] 		= input("regist_name");
			$param['regist_address'] 	= input("regist_address");
			$param['license'] 			= input("license");
			$param['main_business'] 	= input("main_business");
			$param['business_time1'] 	= input("business_time1");
			$param['business_time2'] 	= input("business_time2");
			$param['legal_name'] 		= input("legal_name");
			$param['document_type'] 	= input("document_type");
			$param['document_no'] 		= input("document_no");
			$param['document_time1'] 	= input("document_time1");
			$param['document_time2'] 	= input("document_time2");
			$ress = db('agent_qianyue')->where('agent_id',input("agent_id"))->update($param);
			if($ress){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
		}
    	return $this->fetch();
    }
	//签约流程3
    public function qianyue_step3()
    {
    	$data=db('agent_qianyue')->where('agent_id',input("agent_id"))->find();
		$this->assign('data',$data);
		$this->assign('agent_id',input("agent_id"));
		if(request()->isPost()){
			$param['account_name'] 		= input("account_name");
			$param['bank_account'] 		= input("bank_account");
			$param['open_bank'] 		= input("open_bank");
			$param['branch_address'] 	= input("branch_address");
			$param['branch_name'] 		= input("branch_name");
			$ress = db('agent_qianyue')->where('agent_id',input("agent_id"))->update($param);
			if($ress){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
		}
    	return $this->fetch();
    }
	//签约流程4
    public function qianyue_step4()
    {
    	$data=db('agent_qianyue')->where('agent_id',input("agent_id"))->find();
		$this->assign('data',$data);
		$this->assign('agent_id',input("agent_id"));
		if(request()->isPost()){
			$param['ids_pic1'] 		= input("ids_pic1");
			$param['ids_pic2'] 		= input("ids_pic2");
			$param['license_pic'] 	= input("license_pic");
			$param['bank_pic'] 		= input("bank_pic");
			$param['door_pic'] 		= input("door_pic");
			$param['indoor_pic'] 	= input("indoor_pic");
			$ress = db('agent_qianyue')->where('agent_id',input("agent_id"))->update($param);
			if($ress){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
		}
    	return $this->fetch();
    }
	//签约流程5
    public function qianyue_step5()
    {
    	$data=Db::table('agent_qianyue')
    	->alias('a')
    	->join('sys_platform_type w','a.trade_type = w.id')
    	->field('a.*,w.type_name type_name')
    	->where('a.agent_id',input("agent_id"))
    	->find();
		$info=db('info_agent')->where('id',input("agent_id"))->find();
		$this->assign('data',$data);
		$this->assign('agent_id',input("agent_id"));
		$this->assign('is_state',$info['is_state']);
    	return $this->fetch();
    }
}
